package com.cv4j.app.fragment;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.hist.CalcHistogram;
import com.cv4j.core.hist.CompareHist;

/**
 * 直方图比较-两张完全不同的图界面
 *
 * @since 2021-05-18
 */
public class CompareHist3Fragment {
    private Context mContext;
    private Component component;
    private Image image0;
    private Image image1;
    private Text result;

    private CompareHist3Fragment(Context context) {
        mContext = context;
        init();
    }

    /**
     * CompareHist3Fragment
     *
     * @param context context
     * @return CompareHist3Fragment
     */
    public static CompareHist3Fragment getInstance(Context context) {
        CompareHist3Fragment compareHist2Fragment = new CompareHist3Fragment(context);
        return compareHist2Fragment;
    }

    private void init() {
        component = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_fragment_compare_hist_3, null, false);
        component.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_PARENT));
        image0 = (Image) component.findComponentById(ResourceTable.Id_image0);
        image1 = (Image) component.findComponentById(ResourceTable.Id_image1);
        result = (Text) component.findComponentById(ResourceTable.Id_result);
        initData();
    }

    private void initData() {
        image0.setPixelMap(ResourceTable.Media_test_compare_hist1);
        image1.setPixelMap(ResourceTable.Media_test_compare_hist2);

        CV4JImage cv4jImage1 = new CV4JImage(image0.getPixelMap());
        ImageProcessor imageProcessor1 = cv4jImage1.getProcessor();
        CV4JImage cv4jImage2 = new CV4JImage(image1.getPixelMap());
        ImageProcessor imageProcessor2 = cv4jImage2.getProcessor();
        int[][] source;
        int[][] target;
        CalcHistogram calcHistogram = new CalcHistogram();
        int bins = 256;
        source = new int[imageProcessor1.getChannels()][bins];
        calcHistogram.calcRGBHist(imageProcessor1, bins, source, true);
        target = new int[imageProcessor2.getChannels()][bins];
        calcHistogram.calcRGBHist(imageProcessor2, bins, target, true);
        CompareHist compareHist = new CompareHist();
        StringBuilder sb = new StringBuilder();
        double sum1 = 0.0;
        double sum2 = 0.0;
        double sum3 = 0.0;
        for (int i = 0; i <= 2; i++) {
            sum1 += compareHist.bhattacharyya(source[i], target[i]);
            sum2 += compareHist.covariance(source[i], target[i]);
            sum3 += compareHist.ncc(source[i], target[i]);
        }
        sb.append("巴氏距离:").append(sum1 / 3).append(System.lineSeparator())
            .append("协方差:").append(sum2 / 3).append(System.lineSeparator())
            .append("相关性因子:").append(sum3 / 3);
        result.setText(sb.toString());
    }

    public Component getComponent() {
        return component;
    }
}
